// 128.最长连续序列

package LeetcodeHot100;

import java.util.HashSet;
import java.util.Set;

public class Solution128 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }

        int maxLength = 0;
        for (Integer i : set) {
            // 防止重复计算
            if (set.contains(i - 1))
                continue;
            int length = 0;
            while (set.contains(i)) {
                length++;
                i++;
            }
            if (length > maxLength)
                maxLength = length;
        }
        return maxLength;
    }
}
